home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / music / 7 / pascal / file.doc < prev    next >
Text File  |  1985-11-19  |  9KB  |  255 lines

  1.  
  2.  
  3.  
  4. Disk File Functions                                                     Page 1
  5.  
  6.                              Disk File Operations
  7.  
  8. There  may  be  times when you want to perform operations not supported by the
  9. standard Personal Pascal I/O library.  Until such time as equivalent  routines
  10. are  added  to the Personal Pascal library, you can use direct GEMDOS calls to
  11. achieve the results you want.  For all calls described in this section,  we're
  12. going to assume the following TYPE declarations are present in your program:
  13.  
  14.   TYPE
  15.     Path_Chars = PACKED ARRAY [ 1..80 ] OF Char ;
  16.  
  17. Notice  that  the  maximum  length of a GEMDOS pathname is 80 characters.  The
  18. reason we are using a packed array and not a Pascal STRING type is that GEMDOS
  19. is expecting path and file names in the "C language" string format.  In  other
  20. words,  GEMDOS  normally  wants  a string in which the first byte is the first
  21. data character, and it expects the string to be  terminated  by  a  zero  byte
  22. (Chr(  0  ),  in  Pascal).  You can't pass a Pascal STRING to GEMDOS directly,
  23. since the first character in a Pascal string (s[0]) is the length byte for the
  24. string, and the string is not null-terminated.  In  order  to  pass  a  Pascal
  25. string  to  GEMDOS,  you  have  to copy it into a "C-type" string by calling a
  26. procedure like the following:
  27.  
  28.   PROCEDURE Make_Path( VAR ps : Str255 ; VAR cs : Path_Chars ) ;
  29.  
  30.     VAR
  31.       i : Integer ;
  32.  
  33.     BEGIN
  34.       FOR i := 1 TO Length( ps ) DO
  35.         cs[i] := ps[i] ;
  36.       cs[ length(ps)+1 ] := Chr(0) ;
  37.     END ;
  38.  
  39. Now that we know what type of names to pass to GEMDOS,  we  can  get  on  with
  40. presenting the calls you can use:
  41.  
  42.  
  43. Create and open a file.
  44.  
  45. Sometimes,  you  may  want  open  a  file  with special properties that Pascal
  46. doesn't support.  For this purpose, you can use the following routine:
  47.  
  48. FUNCTION f_create( VAR name : Path_Chars ; attributes : Integer ) : Integer ;
  49.   GEMDOS( $3c ) ;
  50.  
  51. This call creates a new  file  with  the  specified  name  and  the  specified
  52. attributes.    The  bits  in  the  attributes  parameter  have  the  following
  53. assignments:
  54.  
  55. bit  meaning
  56. ---  -------
  57. $01  file is read-only
  58. $02  file is hidden from directory search
  59. $04  file is a system file, hidden from directory search
  60. $08  file contains a volume label in the first 8 data bytes
  61.  
  62. The return value is a valid GEMDOS file handle, if greater than  or  equal  to
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. Disk File Functions                                                     Page 2
  71.  
  72. zero,  or  an  error  number, if negative.  You should use this call to open a
  73. file for output, if you want to open a new file, or if you want to first erase
  74. the previous contents.  If you want to write  to  an  existing  file,  without
  75. erasing the contents, use the f_open call, below.
  76.  
  77.  
  78. Open a file.
  79.  
  80. You  might  also  want  to  open  an  existing  file  (or one you created with
  81. f_create) without using the built-in procedure Reset.  You can use this GEMDOS
  82. call:
  83.  
  84. FUNCTION f_open( VAR name : Path_Chars ; mode : Integer ) : Integer ;
  85.   GEMDOS( $3d ) ;
  86.  
  87. Use this call to open a file for reading, writing, or updating.  If  you  want
  88. to open a file for writing, but you want to first erase the previous contents,
  89. use the f_create call, instead.  The valid values for mode are:
  90.  
  91. 0  open for reading only
  92. 1  open for writing only
  93. 2  open for reading or writing
  94.  
  95. The  return  value is a GEMDOS handle, if greater than or equal to zero, or an
  96. error number, if negative.  Notice that this call does not have a parameter to
  97. specify the  attributes  of  the  file.   Those  attributes  are  set  by  the
  98. f_create  call  and  are  not changed by this call.  If you want to change the
  99. attributes of a file, you can use the f_attrib call, below.
  100.  
  101.  
  102. Close an open file.
  103.  
  104. If you used f_create or f_open to ready a file for access, you should use  the
  105. following  call  to  close  it  when you're finished reading or writing to the
  106. file:
  107.  
  108. FUNCTION f_close( handle : Integer ) : Integer ;
  109.   GEMDOS( $3e ) ;
  110.  
  111. The parameter handle should be the same as that returned  by  the  appropriate
  112. open  call.   Zero  is  returned,  if  the  file was closed successfully, or a
  113. negative error number, otherwise.
  114.  
  115.  
  116. Read bytes from a file.
  117.  
  118. Pascal supports reading from and writing to files one item at  a  time,  where
  119. the  size  of the item is the size of the file pointer variable.  Occasionally
  120. you may want to read or write in larger chunks, especially if your  item  size
  121. is  small,  since  GEMDOS  isn't  very  fast  for  single-byte transfers.  The
  122. following call allows you to read a block of characters into memory:
  123.  
  124. FUNCTION f_read( handle : Integer ; count : Long_Integer ; VAR buf :  Buf_Type
  125. ) : Long_Integer ;
  126.   GEMDOS( $3f ) ;
  127.  
  128. This  call  reads  an  arbitrary  number  of  bytes from a file into a desired
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. Disk File Functions                                                     Page 3
  137.  
  138. buffer.  The number of bytes actually read is returned, if  the  function  was
  139. successful,  or  a  negative error number, if something went wrong.  Note that
  140. the number of bytes actually read may be shorter  than  the  number  of  bytes
  141. requested,  if  the  end-of-file position was reached.  The Buf_Type mentioned
  142. above may be almost any type.  For example, to read 100 two-byte  values  into
  143. an array, you might use a program segment like this:
  144.   TYPE
  145.     Hundred_Integers = ARRAY [ 1..100 ] OF Integer ;
  146.  
  147.   VAR
  148.     a : Hundred_Integers ;
  149.     bytes_read : Long_Integer ;
  150.  
  151.   PROCEDURE  f_read(  handle  :  Integer  ;  count  : Long_Integer ; VAR buf :
  152. Hundred_Integers ) : Long_Integer ;
  153.   GEMDOS( $3f ) ;
  154.  
  155.   BEGIN
  156.     bytes_read := f_read( handle, 200, a ) ;
  157.   END ;
  158.  
  159. Note that 200 was passed as the number of bytes to read, since we  wanted  100
  160. two-byte values!
  161.  
  162. The  handle  parameter should be that value returned by either the f_create or
  163. f_open call.  If you want to use the f_read call to read from a file which was
  164. opened using the built-in procedure Reset, you can use the function Handle  to
  165. find  out the handle associated with the file.  If you are reading from a file
  166. just opened using Reset, you must be aware, however, that the first  item  has
  167. already been read from the file and put into the file buffer variable.
  168.  
  169. Write bytes to a file.
  170.  
  171. Similarly,  you may want to write an arbitrary number of bytes to a file.  The
  172. following call supports block writing:
  173.  
  174. FUNCTION f_write( handle : Integer ; count : Long_Integer ; VAR buf : Buf_Type
  175. ) : Long_Integer ;
  176.   GEMDOS( $40 ) ;
  177.  
  178. This call is the counterpart of the f_read function described above.  It takes
  179. an arbitrary number of bytes from a buffer and outputs them  to  a  previously
  180. opened  file.   The  handle  parameter  must  be  that which was returned by a
  181. previous f_open or f_create call.  You can also use the Handle function to get
  182. the handle of a file which was opened using the  Rewrite  built-in  procedure.
  183. The value returned by f_write is the number of bytes written, if the operation
  184. was  successful,  or  a  negative  error number.  In general, if the number of
  185. bytes returned does not equal the number requested, something went wrong!
  186.  
  187.  
  188. Delete a file.
  189.  
  190. There is no standard procedure in Pascal to remove a file from a disk,  so  if
  191. you want to erase files, you need the following call:
  192.  
  193. FUNCTION f_delete( VAR name : Path_Chars ) : Integer ;
  194.   GEMDOS( $41 ) ;
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. Disk File Functions                                                     Page 4
  203.  
  204.  
  205. Zero  is  returned,  if  the delete was successful, or a negative error value,
  206. otherwise.
  207.  
  208.  
  209. Seek within a file.
  210.  
  211. Personal Pascal supports random access to files using the built-in  procedures
  212. Get,  Put, and Seek.  If you want to use instead the underlying GEMDOS routine
  213. to position within a file, here it is:
  214.  
  215. FUNCTION  f_seek(  offset  :  Long_Integer  ;  handle,  mode  :  Integer  )  :
  216. Long_Integer ;
  217.   GEMDOS( $42 ) ;
  218.  
  219. Use  this  call  to  point  to  a particular byte position within a file.  The
  220. offset parameter specifies the desired byte position, and the  mode  parameter
  221. specifies which file position the offset parameter is relative to:
  222.  
  223. mode  relative to
  224. ----  -----------
  225.   0   the beginning of the file
  226.   1   the current location
  227.   2   the end of the file
  228.  
  229. The  offset  parameter  is  signed,  so  you could, for example, move 10 bytes
  230. backwards in the file by specifying offset and mode parameters of -10  and  1,
  231. respectively.
  232.  
  233.  
  234. Get/Set file attributes.
  235.  
  236. As  mentioned  above,  the  f_create  call  sets  a  file's attributes.  These
  237. attributes are never changed when the file is  subsequently  opened.   If  you
  238. ever  want  to  change  the attributes of a file, you should use the following
  239. call:
  240.  
  241. FUNCTION f_attrib( VAR name : Path_Chars ; mode,  attributes  :  Integer  )  :
  242. Integer ;
  243.   GEMDOS( $43 ) ;
  244.  
  245. The  mode  parameter specifies whether to get the file attributes, if 0, or to
  246. set the attributes, if 1.  The attributes parameter is specified in  the  same
  247. way as for the f_create call, above, with the following two additions:
  248.  
  249. bit  meaning
  250. ---  -------
  251. $10  file is a subdirectory
  252. $20  file is written and closed correctly.
  253.  
  254. These two attributes only refer to subdirectories.
  255. ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆